package org.homeorganiser.flex.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.homeorganiser.flex.dto.ScreenShot;
import org.homeorganiser.flex.util.Constant;

public class ScreenShotDAO implements IScreenShotDAO {

	private final DataSource dataSource;
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement ps;
	
	public ScreenShotDAO(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	public List<ScreenShot> findAll(int type) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<ScreenShot> findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	public ScreenShot findById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	public ScreenShot create(ScreenShot scrnShot) {
		try {
			conn = this.dataSource.getConnection();
			ps = conn.prepareStatement(Constant.SQL_INSERT_SCREENSHOT_TABLE,
					new String[] { "ID" });

			ps.setString(1, scrnShot.getScreenshotName());
			ps.setInt(2, scrnShot.getInventoryType());
			ps.setInt(3, scrnShot.getInventoryId());
			ps.setBytes(4, scrnShot.getImage());
			ps.setString(5,scrnShot.getAddedBy());
			ps.executeUpdate();
			ResultSet rs = ps.getGeneratedKeys();
			rs.next();
			// Update the id in the returned object. This is important as this
			// value must be returned to the client.
			int id = rs.getInt(1);
			scrnShot.setId(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		return scrnShot;
	}

	public boolean update(ScreenShot item) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean remove(ScreenShot item) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean removeAll(int type) {
		// TODO Auto-generated method stub
				return false;
	}
	
	public boolean removeAllScreenShot(int type,int invId){
		try {

			conn = this.dataSource.getConnection();
			ps = conn.prepareStatement(Constant.SQL_DELETE_ALL_SCREENSHOT_BY_INVENTORY);
			ps.setInt(1, type);
			ps.setInt(2, invId);
			return ps.executeUpdate() == 1;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}

	public List<ScreenShot> findScreenshotByItem(int type, int inventoryid) {
		List<ScreenShot> screenshotList = new ArrayList<ScreenShot>();

		ScreenShot scrnshotObj;
		try {

			conn = this.dataSource.getConnection();
			ps = conn.prepareStatement(Constant.SQL_SELECT_ALL_SCREENSHOT_BY_INVENTORY);
			ps.setInt(1, type);
			ps.setInt(2, inventoryid);
			rs = ps.executeQuery();

			while (rs.next()) {
				scrnshotObj = new ScreenShot();
				scrnshotObj.setId(rs.getInt("id"));
				scrnshotObj.setAddedBy(rs.getString("addedBy"));
				scrnshotObj.setScreenshotName(rs.getString("screenshotname"));
				scrnshotObj.setImage(rs.getBytes("image"));
				scrnshotObj.setInventoryId(rs.getInt("inventoryid"));
				screenshotList.add(scrnshotObj);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		return screenshotList;
	}

}
